import 'dart:io';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/ht_appbar.dart';
import 'package:wallet_utils/text_style.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'logic.dart';
class PhotoViewPage extends StatelessWidget {
  static void start(List<String>? imgList, {int? index = 0}) {
    if (imgList?.isEmpty ?? true) return;
    // Get.toNamed(BaseRouterConfig.ninePhotoView,
    //     arguments: {"photoList": imgList, "index": index});
  }

  final logic = Get.put(PhotoViewLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PhotoViewLogic>(builder: (logic) {
      return Scaffold(
        backgroundColor: Colors.black,
        body: Container(
          constraints: BoxConstraints.expand(
            height: MediaQuery.of(context).size.height,
          ),
          child: Stack(
            alignment: Alignment.bottomCenter,
            children: <Widget>[
              PhotoViewGallery.builder(
                scrollDirection: Axis.horizontal,
                scrollPhysics: const BouncingScrollPhysics(),
                builder: (BuildContext context, int index) {
                  return logic.photoList[index].startsWith("images")
                      ? PhotoViewGalleryPageOptions.customChild(
                          child: Image(
                            image: AssetImage(logic.photoList[index]),
                          ),
                          initialScale: PhotoViewComputedScale.contained,
                        )
                      : PhotoViewGalleryPageOptions(
                          imageProvider: _buildItem(index),
                          minScale: PhotoViewComputedScale.contained * 1,
                          maxScale: PhotoViewComputedScale.covered * 5,
                          initialScale: PhotoViewComputedScale.contained,
                        );
                },
                itemCount: logic.photoList.length,
                // loadingChild: widget.loadingChild,
                backgroundDecoration: const BoxDecoration(
                  color: Colors.black,
                ),
                pageController: PageController(initialPage: logic.initialIndex),
                //点进去哪页默认就显示哪一页
                onPageChanged: logic.onPageChanged,
              ),
              Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: htCommonAppbar(
                  context,
                  backgroundColor: Colors.transparent,
                  leading: backWidgetAction(
                      color: Colors.white, backAction: () => Get.back()),
                  title: "",
                ),
              ),
              Container(
                padding: EdgeInsets.only(bottom: Get.bottomBarHeight + 30.w),
                child: Text('${logic.title} / ${logic.photoList.length}',
                    style: TextStyleUtils.styleFFFFFF(fontSize: 17.sp)),
              )
            ],
          ),
        ),
      );
    });
  }

  ImageProvider _buildItem(int index) {
    String path = logic.photoList[index];
    if (path.startsWith("http")) return NetworkImage(path);
    return FileImage(File(path));
  }
}
